﻿@page "/System/User"
@attribute [TabItemOption(Text = "用户管理")]
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@inject MessageService msg

<Table TItem="UserDto" PageItemsSource="@PageItemsSource" AutoGenerateColumns="true"
       IsPagination="true" IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true"  ShowLoading="true" ShowSearch="true"
       OnAddAsync="@OnAddAsync" OnEditAsync="@OnEditAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync"
       OnQueryAsync="@OnQueryAsync" ShowEmpty="true" ShowExportButton="true" ShowDefaultButtons="true" ShowAddButton="AddButton" ShowEditButton="EditButton"
       ShowDeleteButton="DeleteButton" ShowExtendButtons="ShowExtendButtons" ShowExtendEditButton="EditButton" ShowExtendDeleteButton="DeleteButton">
      <TableToolbarTemplate>
        <TableToolbarButton TItem="UserDto"
                                Color="Color.Primary"
                                Icon="fa-fw fa-solid fa-floppy-disk"
                                Text="导入"
                                IsAsync OnClickCallback="@OnImportAsync" />
     </TableToolbarTemplate>
    <RowButtonTemplate>
        <TableCellButton  Color="Color.Success"
                Size="Size.ExtraSmall"
                            Icon="fa-fw fa-solid fa-floppy-disk"
                            Text="下载"
                            IsShow="true"
                             />
     </RowButtonTemplate>
</Table>

    @code {
    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private ITokenContext? Context { get; set; }


    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

    private static bool AddButton { get; set; }

    private static bool EditButton { get; set; }

    private static bool DeleteButton { get; set; }

    private static bool ShowExtendButtons { get; set; } = false;

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        var buttons = Context.GetSession<List<MenuDto>>(ClientAppKey.UserPermissions).Where(o => o.ParentCode == "1101");

        AddButton = buttons.Any(o => o.MenuCode == "btnAdd");
        EditButton = buttons.Any(o => o.MenuCode == "btnEdit");
        DeleteButton = buttons.Any(o => o.MenuCode == "btnDelete");
        if(EditButton || DeleteButton)
        {
            ShowExtendButtons = true;
        }
    }

    /// <summary>
    /// 新增前
    /// </summary>
    /// <returns></returns>
    private static async Task<UserDto> OnAddAsync()
    {
        // 模拟延时

        return new UserDto() { };
    }

    /// <summary>
    /// 修改前
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    private static async Task<UserDto> OnEditAsync(UserDto item)
    {
        // 模拟延时
        return item;
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="item"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    private async Task<bool> OnSaveAsync(UserDto item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Add)
        {
            item.Creater = Context.CurrentUser.Account;
            item.CreatedName = Context.CurrentUser.UserName;
            await Global.ExecuteApiAsync(() => Request.System.AddUser(item));
        }
        else
        {
            item.Modifier = Context.CurrentUser.UserName;
            item.ModifiedName = Context.CurrentUser.UserName;
            await Global.ExecuteApiAsync(() => Request.System.UpdateUserAsync(item));
        }

        return true;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    private async Task<bool> OnDeleteAsync(IEnumerable<UserDto> items)
    {
        await Global.ExecuteApiAsync(() => Request.System.DeleteMutiUserAsync(items.ToList()));
        return true;
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="options"></param>
    /// <returns></returns>
    private async Task<QueryData<UserDto>> OnQueryAsync(QueryPageOptions options)
    {
        try
        {
            var model = await Request.System.QueryUserAsync(QueryParameter.GetQueryParameter(options));
            return model.data;
        }
        catch (Exception ex)
        {
            return new QueryData<UserDto>();
        }


    }

    private async Task OnImportAsync(IEnumerable<UserDto> item)
    {
        
    }
}
